﻿/*
 * RegExpress
 * 
 * Copyright (c) 2010, Daniel McGaughran
 * 
 * Licensed under the Apache Licence, Version 2.0 (the "Licence");
 * you may not use this file except in compliance with the Licence.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 * 
 */

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace XrControls
{
    /// <summary>
    /// Encapsulates a binary setter, where value can be set / cleared via explicit buttons;
    /// or be overridden to also support basic ToggleButton functionality.
    /// </summary>
    [TemplatePart(Name = "P_SetButton", Type = typeof(ButtonBase))]
    [TemplatePart(Name = "P_ClearButton", Type = typeof(ButtonBase))]
    [TemplatePart(Name = "P_ToggleButton", Type = typeof(ButtonBase))]
    public class ToggleSwitch : ContentControl
    {
        public static readonly DependencyProperty ValueProperty;
        public static readonly DependencyProperty IsReadOnlyProperty;

        static ToggleSwitch()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ToggleSwitch),
                new FrameworkPropertyMetadata(typeof(ToggleSwitch)));

            ValueProperty = DependencyProperty.Register("Value", typeof(bool), typeof(ToggleSwitch),
                new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
            IsReadOnlyProperty = DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(ToggleSwitch),
                new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
        }



        public bool Value 
        {
            get { return (bool)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }


        public bool IsReadOnly
        {
            get { return (bool)GetValue(IsReadOnlyProperty); }
            set { SetValue(IsReadOnlyProperty, value); }
        }



        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            ButtonBase setButton = GetTemplateChild("P_SetButton") as ButtonBase;
            ButtonBase clearButton = GetTemplateChild("P_ClearButton") as ButtonBase;
            ButtonBase toggleButton = GetTemplateChild("P_ToggleButton") as ButtonBase;

            if (setButton != null)
                setButton.Click += setButton_Click;

            if (clearButton != null)
                clearButton.Click += clearButton_Click;

            if (toggleButton != null)
                toggleButton.Click += toggleButton_Click;
        }


        
        private void setButton_Click(object sender, RoutedEventArgs e)
        {
            if(!IsReadOnly)
                Value = true;
        }



        private void clearButton_Click(object sender, RoutedEventArgs e)
        {
            if (!IsReadOnly)
                Value = false;
        }



        private void toggleButton_Click(object sender, RoutedEventArgs e)
        {
            if (!IsReadOnly)
                Value = !Value;
        }


    }
}
